﻿package com.vinyl.contents
{
    import flash.display.Sprite;
    import flash.events.NetStatusEvent;
    import flash.events.SecurityErrorEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.Event;

    public class SimpleFlv extends Sprite
	{
        private var videoURL:String;
        private var connection:NetConnection;
        private var stream:NetStream;
		
		public var initFlag:Boolean;
		public var video:Video;
		public var totalframes;
		public var dur;
		public var st;
		public var rate;
		public var idx:Number;
		
        public function SimpleFlv() 
		{
            connection = new NetConnection();
            connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            connection.connect(null);
        }
		
		public function play( url )
		{
			initFlag = false;
			videoURL = url;
			stream.play(videoURL);
		}
		
		public function pause()
		{
			stream.pause();
		}
		
		public function resume()
		{
			stream.resume();
		}
		
		public function get _stream():NetStream
		{
			return stream;
		}
		
        private function netStatusHandler(event:NetStatusEvent):void {
            switch (event.info.code) {
                case "NetConnection.Connect.Success":
                    connectStream();
                    break;
                case "NetStream.Play.StreamNotFound":
                    trace("Stream not found: " + videoURL);
                    break;
				case "NetStream.Play.Stop" : 
					dispatchEvent( new Event( "playBackComplete" ) );
					break;
            }
        }
		
        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }
		
        private function connectStream():void {
            stream = new NetStream(connection);
            stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			
			var custom_obj:Object = new Object(); 
			//custom_obj.onPlayStatus = playStatus; 
			//custom_obj.onMetaData = onMetaData; 
			stream.client = this; 
			
            video = new Video();
            video.attachNetStream(stream);
            //stream.play(videoURL);
            addChild(video);
			
        }
		
		private function playStatus(event:Object):void { 
			switch (event.info.code) {               
				case "NetStream.Play.Complete":                  
				break;       
			} 
		} 
		
		public function onMetaData(info:Object):void {
			if ( initFlag ) return;
			//trace("metadata: duration1=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
			video.width = info.width;
			video.height = info.height;
			
			dur = info.duration;
			rate = info.framerate;
			totalframes = Math.round( info.framerate *  info.duration );
			
			dispatchEvent( new Event( "load" ) );
			
			initFlag = true;
		}
		
		public function onCuePoint(info:Object):void {
			//trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
		}
    }
}





